package com.jmxcfc.blfsc.huisheng.thread;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jmxcfc.blfsc.common.sms.enums.MsgTypeEnum;
import com.jmxcfc.blfsc.common.sms.enums.SendStatusEnum;
import com.jmxcfc.blfsc.common.sms.notify.NotifyDataItem;
import com.jmxcfc.blfsc.common.sms.notify.ResultNotifyRequest;
import com.jmxcfc.blfsc.huisheng.common.Constants;
import com.jmxcfc.blfsc.huisheng.entity.SendDetailSub;
import com.jmxcfc.blfsc.huisheng.entity.VoiceNotifyRecord;
import com.jmxcfc.blfsc.huisheng.feign.ISmsClientService;
import com.jmxcfc.blfsc.huisheng.util.SpringBeanUtils;
import com.jmxcfc.fs.common.IntegerEnum;
import com.jmxcfc.fs.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *异步发起调用blfs-sms 进行发送状态(语音发送状态) 通知
 */
@Slf4j
public class SyncSendNotifyResultOperator implements Runnable {
    private String caseId;
    private SendDetailSub sendDetailSub;
    private VoiceNotifyRecord voiceNotifyRecord;

    /**
     * 数据推送回调(语音外呼), voiceNotifyRecord必传值
     * @param caseId
     * @param sendDetailSub
     * @param voiceNotifyRecord
     */
    public SyncSendNotifyResultOperator(String caseId,SendDetailSub sendDetailSub,VoiceNotifyRecord voiceNotifyRecord) {
        this.caseId = caseId;
        this.sendDetailSub = sendDetailSub;
        this.voiceNotifyRecord= voiceNotifyRecord;
    }

    @Override
    public void run() {
        try {
            ISmsClientService smsClientService = SpringBeanUtils.getBean(ISmsClientService.class);
            ResultNotifyRequest<JSONObject> resultNotifyRequest = buildNotifyParam(sendDetailSub, voiceNotifyRecord);
            log.info("异步线程处理: 慧声语音-回调处理,caseId【标识一条短信的流水号uuid】:{},发送状态异步通知调用blfs-sms,调用参数:{}", caseId,JSON.toJSONString(resultNotifyRequest));
            Result<Object> result = smsClientService.receiveNotify(JSON.parseObject(JSON.toJSONString(resultNotifyRequest)));
            log.info("异步线程处理:慧声语音-回调处理,tag【标识一条短信的流水号uuid】:{},发送状态异步通知调用blfs-sms,响应结果:{}",caseId,result);
        } catch (Exception e) {
            log.error("慧声语音-回调处理,caseId【标识一条短信的流水号uuid】:{},发送状态异步通知处理异常,调用blfs-sms 异常,异常信息:{}", caseId, e.getMessage(), e);
        }

    }

    /**
     * 参数组织
     * 1、挂机短信回调接口, smsNotifyRecord必传值
     * 2、ai挂断回调(语音外呼), voiceNotifyRecord必传值
     * @param sendDetailSub
     * @param voiceNotifyRecord
     * @return
     */
    private ResultNotifyRequest<JSONObject> buildNotifyParam(SendDetailSub sendDetailSub, VoiceNotifyRecord voiceNotifyRecord) {
        ResultNotifyRequest<JSONObject> notifyRequest = ResultNotifyRequest.of(MsgTypeEnum.VOICE);
        if (Objects.nonNull(sendDetailSub)) {
            notifyRequest.setRequestNo(sendDetailSub.getRequestNo());
            notifyRequest.setBatchId(sendDetailSub.getRequestId());
        }
        NotifyDataItem notifyDataItem = new NotifyDataItem();
        if (Objects.nonNull(voiceNotifyRecord)) {
            //数据推送回调(外呼回调)
            notifyDataItem.setUuid(voiceNotifyRecord.getCaseId());
            notifyDataItem.setMobile(voiceNotifyRecord.getPhoneNumber());


            //语音最终状态 (结果：已接听标识成功，其他情况标识失败)
            notifyDataItem.setVoiceSendStatus(StringUtils.equals(voiceNotifyRecord.getCallStatus(), Constants.STATUS_SUCCESS) ? SendStatusEnum.SUCCESS : SendStatusEnum.FAILURE);
            notifyDataItem.setVoiceSendStatusDesc(notifyDataItem.getVoiceSendStatus().desc());
            log.info("慧声语音-【数据推送回调】,caseId【标识一条短信的流水号uuid】:{},语音外呼行动码:{},转换成JMX发生状态:{}", voiceNotifyRecord.getId(), voiceNotifyRecord.getResult(), notifyDataItem.getVoiceSendStatus());
            //行动码
            notifyDataItem.setErrorCode(voiceNotifyRecord.getActionCode());
            //行动描述
            notifyDataItem.setErrorDesc(voiceNotifyRecord.getActionMsg());
        }

        List<NotifyDataItem> notifyDataItemList = new ArrayList<>(IntegerEnum.INTEGER_1.value());
        notifyDataItemList.add(notifyDataItem);
        notifyRequest.setNotifyDataList(notifyDataItemList);
        notifyRequest.setNotifyDataList(notifyDataItemList);
        notifyRequest.setEndTime(LocalDateTime.now());
        return notifyRequest;
    }



}
